package inherit;

//子类构造方法
//父类
class Animal {
    public String name;
    public void sleep() {
        System.out.println(name+"正在睡觉");
    }

    //默认提供的父类构造方法
//    public Animal() {
//
//    }
    //父类构造方法
    public Animal(String name) {
        this.name = name;
        System.out.println("Animal(String )");
    }
}
//Dog子类
class Dog extends Animal {
    //子类独有的属性和方法
    public String name;
    public boolean loyal;

    public void func() {
        System.out.println(super.name + "是忠诚的: " + loyal);
        System.out.println(this.name + "是忠诚的: " + loyal);

    }
    //默认提供的子类构造方法
//    public Dog() {
//        super();
//    }
    //子类构造方法
    public Dog(String name1, boolean loyal) {
        super("旺财");       //1、帮助父类进行初始化，如果父类有name和age两个值可以：super(name,age)
        this.name = name1; //2、子类自己初始化
        this.loyal = loyal;
        System.out.println("Dog(String , boolean )");
    }
}
public class Test {
    public static void main(String[] args) {
        Dog dog = new Dog("hello",true);
        dog.func();
    }
}

//静态块，实例块，构造方法执行顺序
//父类
class Animal {
    public String name;

    static {
        System.out.println("static Animal()!");
    }
    {
        System.out.println("example Animal()!");
    }
    public Animal() {
        System.out.println("public Animal()!");
    }
}
//Dog子类
class Dog extends Animal {
    //子类独有的属性
    public boolean loyal;
    static {
        System.out.println("static Dog()!");
    }
    {
        System.out.println("example Dog()!");
    }
    public Dog() {
        System.out.println("public Dog()!");
    }

}

public class Test {
    public static void main(String[] args) {
        Dog dog = new Dog();
        System.out.println("--------------------------");
        Dog dog2 = new Dog();
    }
}


